package com.weme.mine.dialog

import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.InputFilter
import android.text.TextUtils
import android.text.TextWatcher
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewTreeObserver
import android.view.inputmethod.InputMethodManager
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.FragmentActivity
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.weme.mine.databinding.DialogEdNameBinding

interface EdNameDialogListener {
    fun onNameEntered(name: String)
}

class EdNameDialog(var mContext : FragmentActivity, var TYPE :Int, val content :String) :BottomSheetDialog(mContext,com.weme.common.R.style.TUIBeautyDialogTheme) {
    private lateinit var binding: DialogEdNameBinding
    private var listener: EdNameDialogListener? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogEdNameBinding.inflate(LayoutInflater.from(context), null, false)
        setContentView(binding.root)
        ImmersionBar.with(mContext).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).init()
        binding.edName.setText(content)
        binding.edName.setSelection(binding.edName.text.length)
        binding.tvSum.text = if(TYPE == 0) (15 - binding.edName.text.length).toString() else (140 - binding.edName.text.length).toString()
        if(TYPE == 0){
            binding.srvEdit.updateLayoutParams {
                this.height = 100
            }
            binding.tvSum.gravity = Gravity.CENTER_VERTICAL
            binding.edName.gravity = Gravity.CENTER_VERTICAL

            binding.tvName.text = binding.edName.context.getString(com.example.resources.R.string.ed_user_nickname)
            binding.edName.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(15))
        }else{
            binding.srvEdit.updateLayoutParams {
                this.height = 187
            }
            binding.edName.updateLayoutParams<ConstraintLayout.LayoutParams> {
                this.topMargin = context.resources.getDimensionPixelOffset(com.example.resources.R.dimen.dp_12)
            }
            binding.tvSum.gravity = Gravity.BOTTOM
            binding.edName.gravity = Gravity.TOP
            binding.edName.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(140))
            binding.tvName.text = binding.edName.context.getString(com.example.resources.R.string.ed_user_about)
        }
        binding.edName.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                binding.tvSum.text = if(TYPE == 0) (15 - binding.edName.text.length).toString() else (140 - binding.edName.text.length).toString()
                binding.smvBtn.alpha = if (binding.edName.text.toString().isNotEmpty()&&!TextUtils.equals(content,binding.edName.text.toString())) 1F else 0.6F
            }
        })
        binding.smvBtn.setOnClickListener {
            if(binding.smvBtn.alpha == 1F){
                val name = binding.edName.text.toString()
                listener?.onNameEntered(name)
                dismiss() // 关闭对话框
            }
        }
        binding.root.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                binding.root.viewTreeObserver.removeOnGlobalLayoutListener(this)
                binding.edName.requestFocus()
                binding.edName.postDelayed({ // 延迟显示软键盘，确保对话框已经完全布局完成
                    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.showSoftInput(binding.edName, InputMethodManager.SHOW_IMPLICIT)
                },200)
            }
        })

    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        window?.setWindowAnimations(com.weme.common.R.style.SlideUpAnimation)
    }


    fun setEdNameDialogListener(listener: EdNameDialogListener) {
        this.listener = listener
    }


}